<template>
  <div class="category-page">
    <div class="content-wrapper">
      <!-- 分类头部 -->
      <div class="category-header">
        <div class="header-left">
          <el-icon class="category-icon"><component :is="currentCategory?.icon" /></el-icon>
          <h1>{{ currentCategory?.name }}</h1>
        </div>
        <div class="header-info">
          <span class="tool-count">{{ categoryTools.length }}个工具</span>
        </div>
      </div>

      <!-- 筛选区域 -->
      <div class="filter-section">
        <el-input
          v-model="searchQuery"
          placeholder="搜索工具..."
          class="search-input"
          :prefix-icon="Search"
        />
        <el-select v-model="sortBy" class="sort-select">
          <el-option label="最新添加" value="newest" />
          <el-option label="最多使用" value="uses" />
          <el-option label="最多收藏" value="likes" />
        </el-select>
      </div>

      <!-- 工具列表 -->
      <div class="tool-grid">
        <div 
          v-for="tool in filteredTools" 
          :key="tool.path"
          class="tool-card"
          @click="router.push(tool.path)"
        >
          <div class="tool-header">
            <div class="tool-icon">
              <el-icon><component :is="tool.icon" /></el-icon>
            </div>
            <el-tag 
              :type="getTagType(tool.category)" 
              class="tool-tag"
              effect="plain"
            >
              {{ tool.category }}
            </el-tag>
          </div>
          <div class="tool-info">
            <h3>{{ tool.name }}</h3>
            <p>{{ tool.description }}</p>
          </div>
          <div class="tool-footer">
            <div class="tool-stats">
              <span class="stat-item">
                <el-icon><View /></el-icon>
                {{ formatNumber(tool.uses) }}
              </span>
              <span class="stat-item">
                <el-icon><Star /></el-icon>
                {{ formatNumber(tool.likes) }}
              </span>
            </div>
            <el-button class="use-btn" type="primary" text>
              立即使用
              <el-icon><ArrowRight /></el-icon>
            </el-button>
          </div>
        </div>
      </div>

      <!-- 无数据提示 -->
      <el-empty
        v-if="filteredTools.length === 0"
        description="暂无相关工具"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { mainMenus } from '@/router/menus'
import { ArrowRight, View, Star, Search } from '@element-plus/icons-vue'
import { toolsData } from '@/mock/tools'

const route = useRoute()
const router = useRouter()

const searchQuery = ref('')
const sortBy = ref('newest')

// 获取当前分类信息
const currentCategory = computed(() => {
  return mainMenus.find(menu => menu.path === route.path)
})

// 获取当前分类的工具
const categoryTools = computed(() => {
  return toolsData.filter(tool => tool.category === currentCategory.value?.name)
})

// 过滤和排序后的工具列表
const filteredTools = computed(() => {
  let tools = [...categoryTools.value]
  
  // 搜索过滤
  if (searchQuery.value) {
    tools = tools.filter(tool => 
      tool.name.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
      tool.description.toLowerCase().includes(searchQuery.value.toLowerCase())
    )
  }

  // 排序
  switch (sortBy.value) {
    case 'uses':
      tools.sort((a, b) => b.uses - a.uses)
      break
    case 'likes':
      tools.sort((a, b) => b.likes - a.likes)
      break
    default:
      // 默认按最新排序（这里假设有 id 字段表示添加顺序）
      tools.sort((a, b) => b.id - a.id)
  }

  return tools
})

// 格式化数字
const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + 'w'
  }
  return num.toString()
}

// 获取标签类型
const getTagType = (category) => {
  const types = {
    'AI对话': '',
    '图像处理': 'success',
    '文本处理': 'warning',
    '视频处理': 'danger',
    '语音处理': 'info'
  }
  return types[category] || ''
}
</script>

<style scoped>
.category-page {
  background: #f8fafc;
  min-height: 100vh;
  padding: 24px;
  box-sizing: border-box;
}

.content-wrapper {
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

.category-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.category-icon {
  font-size: 32px;
  color: #0ea5e9;
}

.header-left h1 {
  font-size: 32px;
  font-weight: 600;
  color: #0f172a;
  margin: 0;
}

.tool-count {
  color: #64748b;
  font-size: 14px;
  background: #f1f5f9;
  padding: 4px 12px;
  border-radius: 16px;
}

.filter-section {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
}

.search-input {
  width: 300px;
}

.sort-select {
  width: 120px;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 24px;
}

.tool-card {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.tool-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.05);
  border-color: #0ea5e9;
}

.tool-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.tool-icon {
  width: 40px;
  height: 40px;
  background: #f1f5f9;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tool-icon .el-icon {
  font-size: 24px;
  color: #0ea5e9;
}

.tool-tag {
  font-size: 12px;
}

.tool-info {
  margin-bottom: 16px;
  flex: 1;
}

.tool-info h3 {
  font-size: 16px;
  font-weight: 600;
  color: #0f172a;
  margin: 0 0 8px 0;
}

.tool-info p {
  font-size: 14px;
  color: #64748b;
  margin: 0;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.tool-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: auto;
}

.tool-stats {
  display: flex;
  gap: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #64748b;
  font-size: 14px;
}

.use-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 16px;
  height: 32px;
  box-sizing: border-box;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .category-page {
    padding: 16px;
  }

  .content-wrapper {
    padding: 0;
  }

  .category-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
    padding: 0 0 16px 0;
  }

  .header-left h1 {
    font-size: 24px;
  }

  .filter-section {
    flex-direction: column;
    padding: 0;
    margin-bottom: 16px;
  }

  .search-input,
  .sort-select {
    width: 100%;
  }

  .tool-grid {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 0;
  }

  .tool-card {
    margin: 0;
    width: 100%;
    box-sizing: border-box;
  }

  .tool-footer {
    flex-direction: row !important;
    align-items: center !important;
    margin-top: auto;
    width: 100%;
  }

  .tool-card {
    display: flex;
    flex-direction: column;
    height: auto;
    min-height: 200px;
  }

  .use-btn {
    padding: 8px 16px;
    height: 32px;
    margin-left: auto;
    width: auto !important;
    justify-content: flex-end !important;
  }

  .tool-stats {
    flex-shrink: 0;
  }
}

@media (max-width: 480px) {
  .category-page {
    padding: 12px;
  }

  .tool-footer {
    flex-direction: row !important;
    justify-content: space-between;
    gap: 8px;
  }

  .use-btn {
    width: auto !important;
    justify-content: center;
  }

  .tool-stats {
    gap: 12px;
  }

  .tool-info h3 {
    font-size: 15px;
  }

  .tool-info p {
    font-size: 13px;
  }
}
</style> 